<?php 

Class LoginAction extends _loginAction{
    /**
     * 注册页面
     * @return [type] [description]
     */
    public function register()
    {
        $this->display('register');
    }

    /**
     * 判断用户名是否已经存在
     * 返回json数据，如果已经存在，status为0，否则为1
     * @return boolean [description]
     */
    public function isUserExistHandle()
    {
        if (!IS_POST) halt("页面不存在！");
        $username = I('username');
        if (D('User')->isUserExist($username)) {
            $this->ajaxReturn(null,"",1);
        } else {
            $this->ajaxReturn(null,"",0);
        }
    }

    /**
     * 注册处理函数
     * @return [type] [description]
     */
    public function registerHandle()
    {
        
        if (!IS_POST) halt("页面不存在！");
        $data['username'] = I('username');
        $data['password'] = encode(I('password'));
        $data['email'] = I('email');
        $data['regtime'] = date("Y-m-d H:i:s");
        $data['regip'] = get_client_ip();
        $data['status'] = 1;
        $verify = I('verify');
        
        if (strlen(I('password')) < 6) {
            $this->error('密码长度过短！为了您账号的安全，请设置密码长于6个字符！');
        }
        // p($data);

        if (D('User')->addUser($data)) {
            $this->success('注册成功！正在跳转到登陆页面！', U(GROUP_NAME . '/Login/index'));
        } else {
            $this->error('注册失败！');
        }
    }

    /**
     * display forget password page
     * @return [type] [description]
     */
    public function forgetPassword()
    {
        $this->display('forgetPassword');
    }

    /**
     * handle forget password process, 
     * first judge username and email is matched 
     * if matched , create a new randomize password and send it to 
     * the email 
     * @return [type] [description]
     */
    public function forgetPasswordHandle()
    {
        if (!IS_POST) halt("页面不存在！");

        $email = I('email');
        $username = I('username');

        if (strlen(I('password')) < 6) {
            $this->error('密码长度过短！为了您账号的安全，请设置密码长于6个字符！');
        }

        if (md5($verify) != session('verify')) {
            $this->error('验证码错误！');
        }

        import('ORG.Util.String');
        $newPasswd = String::randString();
        D('User')-> where('username=' . $username)->setField('password',encode($newPasswd));

        import('ORG.Email');//导入本类
        $data['mailto'] = $email; //收件人
        $data['subject'] =  "IS-DS 找回密码"; //邮件标题
        $data['body'] = "
        这是一封忘记密码邮件，<br>
        您的新密码为 $newPasswd, <br>
        请登录后重新修改密码！
        "; //邮件正文内容
        $mail = new Email();
        if($mail->send($data)) {
            $this->success('邮件发送成功！', 'index');
        } else {
            $this->error('邮件发送失败！请检查网络连接或重新发送！');
        }
    }
}
 ?>